from .EnvParser import *

__all__ = [
    'setFontSize', # 设置字体大小
    'setPython3Env', # 选择设置虚拟环境
    'setPlatfrom', # 设置当前运行平台

    'add_rootpath_for_obj', # 添加根路径
    'add_alias_for_files', # 添加文件别名
]

def setFontSize(step = 1, method = 'add'):
    """设置字体大小"""
    obj: EnvParser = ExecEnvParser()
    node = obj.get_xpath_node('page/font-size')
    font_size = int(node.text)
    if 'add' == method: 
        node.text = str(font_size + step)
    else:
        if font_size > 2:
            node.text = str(font_size - step)
    obj.save()

def setPython3Env(path: str):
    """选择设置虚拟环境"""
    obj = ExecEnvParser()
    node = obj.get_xpath_node('env/python3')
    node.text = str(path)
    obj.save()

def setPlatfrom(name: str):
    """设置当前运行平台"""
    obj = ExecEnvParser()
    node = obj.get_xpath_node('env/platform')
    node.text = str(name)
    obj.save()

def add_rootpath_for_obj(paths: List[str], op_type: str):
    """添加根路径"""
    obj = ExecEnvParser()
    node = obj.get_xpath_node(f'paths/obj[name={op_type}]')
    for path in paths:
        temp_node = ET.SubElement(node, 'li')
        temp_node.text = path
    obj.save()

def add_alias_for_files(alias: List[str], file_type: str='urls'):
    '''添加文件别名'''
    obj = ExecEnvParser()
    node: ET.Element = obj.get_xpath_node(f'alias/file[name={file_type}]')

    for li in list(node.iter(tag='li'))[::-1]:
        node.remove(li)

    for ali in alias:
        temp_node = ET.SubElement(node, 'li')
        temp_node.text = ali
    obj.save()
